package com.czl.lunt.controller;

import com.czl.lunt.common.Result;
import com.czl.lunt.common.ResultCode;
import com.czl.lunt.config.AppConfig;
import com.czl.lunt.model.User;
import com.czl.lunt.service.UserService;
import com.czl.lunt.utils.MD5Utils;
import com.czl.lunt.utils.UUIDUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/user")
@Tag(name = "User", description = "用户类接口")
public class UserController {
    @Autowired
    private UserService userService;


    //@Validated 开启参数校验 校验失败抛出BindException异常
     @PostMapping("/register")
     @Operation(description = "注册user信息")
     //可以写DTO但是我不会写前端 后面改
     public Result register(@NonNull String username,
                            @NonNull String password,
                            @NonNull String nickname,
                            @NonNull String passwordRepeat){
         //前后密码不一致
         if(!password.equals(passwordRepeat)){
             log.error("前后密码不一致");
             return Result.failed(ResultCode.FAILED_TWO_PWD_NOT_SAME);
         }
         User user = new User();
         user.setUsername(username);
         user.setPassword(MD5Utils.md5(username));
         user.setNickname(nickname);
         //生成一个密码盐
         String salt = UUIDUtils.UUID_32();
         String ePassword = MD5Utils.md5Salt(password, salt);
         user.setPassword(ePassword);
         user.setSalt(salt);

         userService.createUser(user);
         return Result.success(ResultCode.SUCCESS);
     }

    @Operation(description = "登录函数")
     @PostMapping("/login")
     public Result login(HttpServletRequest request,
                         @RequestParam("username") @NonNull String username,
                         @RequestParam("password") @NonNull String password){
         User user = userService.login(username, password);
         if(user == null){
             log.info("登录失败");
             return Result.failed(ResultCode.FAILED_LOGIN);
         }
         // true 没有session的时候重新开启一个session
         HttpSession session = request.getSession(true);
         session.setAttribute(AppConfig.USER_SESSION, user);
         return Result.success(ResultCode.SUCCESS);
     }

    @Operation(description = "退出登录")
    @GetMapping("/logout")
    public Result logout(HttpServletRequest request){
        HttpSession session = request.getSession(false);
        if(session != null){
            //消除session对象 和关联数据
            session.invalidate();
        }
         return Result.success("退出成功");
    }

     @Operation(description = "根据id获取用户信息")
     @GetMapping("/info")
     public Result getUserInfo(HttpServletRequest request,
                               @RequestParam(value = "id", required = false) Long id){
         //id为空的时候从session中获取用户信息， 也就是当前用户的信息
         User user = null;
         if(id == null){
             //session不存在也不去进行session的创建
             HttpSession session = request.getSession(false);
             //判断session是否存在
             if(session == null || session.getAttribute(AppConfig.USER_SESSION) == null){
                 //用户没有登录
                 return Result.failed(ResultCode.FAILED_LOGIN);
             }
             //通过校验
             user = (User) session.getAttribute(AppConfig.USER_SESSION);
         }else{
             user = userService.selectById(id);
         }
         if(user == null){
             return Result.failed(ResultCode.FAILED_LOGIN);
         }
         //不为空获取id用户的信息
         return Result.success(user);
     }




}
